/* 
 * Created on 17. September 2008, 23:38
 *
 *      Copyright 2008 Oliver Eichner <o.eichner@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include <stdlib.h>


 /**
 * @file SaveToFileDialog.cpp
 * Save Dialog for saving device informations to a text file
 */

#include "SaveToFileDialog.h"

/**
 * @brief Savedialog constructor
 * @param nDevice Iterator for device list
 */
SaveToFileDialog::SaveToFileDialog(Device nDevice)
: Gtk::Dialog("Save to File", true) {
    set_border_width(8);
    add_button("Add Comment", Gtk::RESPONSE_APPLY);
       add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
    add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
 
    

    sv_FileChooser.set_action(Gtk::FILE_CHOOSER_ACTION_SAVE);

    Gtk::FileFilter filter_txt;
    filter_txt.set_name("Text file");
    filter_txt.add_mime_type("text/plain");
    sv_FileChooser.add_filter(filter_txt);

    Gtk::FileFilter filter_any;
    filter_any.set_name("Any file");
    filter_any.add_pattern("*");
    sv_FileChooser.add_filter(filter_any);

    sv_FileChooser.set_current_folder(getenv("HOME"));
    //set example filename
    string fname;
    if (!nDevice.getBridge_chan_index().empty()) {
        if (!nDevice.getBridge_drive_vendor().empty()){
            fname.append(nDevice.getBridge_drive_vendor());
            fname.append("-");
        }
        fname.append(nDevice.getBridge_drive_model());
    } else {
        if (!nDevice.getVendor().empty()){
            fname.append(nDevice.getVendor());
            fname.append("-");
        }
        fname.append(nDevice.getModel());
    }
    fname.append(".txt");
    sv_FileChooser.set_current_name(fname);


    get_vbox()->add(sv_FileChooser);

    show_all();
}

SaveToFileDialog::~SaveToFileDialog() {
}

/**
 * @brief Save device informations to a file
 * @param filename The filename save to
 * @param deviceIterator Iterator for device list
 */
void SaveToFileDialog::saveTo(string filename, Device nDevice) {
    fstream FileSaveTo(filename.data(), ios::out);
    //get current time 
    time_t t;
    time(&t);
    string time = ctime(&t);


    cout << "Device to save: " << nDevice.getDeviceName();
    cout << "File to save: " << filename << endl;

    //head
    FileSaveTo << "Device Informations (/dev/" << nDevice.getDeviceName() << "):\n"
            << "-------------------------------\n"
            << "Createt with " << TITLE << " " << VER << " on " << time << "\n";
    //Comment
    if (!nDevice.getComment().empty()) {
        FileSaveTo << "## Comment ##\n"
                << nDevice.getComment() << "\n"
                << "\n";


    }
    //body
    if (!nDevice.getBridge_chan_index().empty()) {
        FileSaveTo << "## Bridge Information ##\n"
                << "Channel Index:\t\t" << nDevice.getBridge_chan_index() << "\n"
                << "Channel Type:\t\t" << nDevice.getBridge_chan_type() << "\n"
                << "Writes Permitted:\t" << (nDevice.getBridge_writes_permitted() ? TP_YES : TP_NO) << "\n"
                << "Declare Write Blocked:\t" << (nDevice.getBridge_declare_write_blocked() ? TP_YES : TP_NO) << "\n"
                << "Declare Write Errors:\t" << (nDevice.getBridge_declare_write_errors() ? TP_YES : TP_NO) << "\n"
                << "Bridge Serial:\t\t" << nDevice.getBridge_serial() << "\n"
                << "Bridge Vendor:\t\t" << nDevice.getBridge_vendor() << "\n"
                << "Bridge Model:\t\t" << nDevice.getBridge_model() << "\n"
                << "Firmware Date:\t\t" << nDevice.getBridge_firmware_date() << "\n"
                << "Firmware Time:\t\t" << nDevice.getBridge_firmware_time() << "\n"
                << "\n"
                << "## Drive Information ##\n"
                << "Drive Vendor:\t\t" << nDevice.getBridge_drive_vendor() << "\n"
                << "Drive Model:\t\t" << nDevice.getBridge_drive_model() << "\n"
                << "Drive Serial:\t\t" << nDevice.getBridge_drive_serial() << "\n"
                << "Drive Revision:\t\t" << nDevice.getBridge_drive_revision() << "\n"
                << "Drive Size:\t\t" << nDevice.getSize() << "\n"
                << "Drive BlockSize\t\t" << nDevice.getBridge_drive_capacity() << "\n"
                << "\n"
                << "## Drive HPA/DCO/Security Information ##\n"
                << "Security Support:\t" << (nDevice.getBridge_security_support() ? TP_YES : TP_NO) << "\n";
        if (nDevice.getBridge_security_support()) {
            FileSaveTo << "Security In Use:\t" << (nDevice.getBridge_security_in_use() ? TP_YES : TP_NO) << "\n";
        }
        FileSaveTo << "HPA Support:\t\t" << (nDevice.getBridge_hpa_support() ? TP_YES : TP_NO) << "\n";
        if (nDevice.getBridge_hpa_support()) {
            FileSaveTo << "HPA In Use:\t\t" << (nDevice.getBridge_hpa_in_use() ? TP_YES : TP_NO) << "\n"
                    << "HPA Capacity:\t\t" << nDevice.getBridge_hpa_capacity() << "\n";

        }
        FileSaveTo << "DCO Support:\t\t" << (nDevice.getBridge_dco_support() ? TP_YES : TP_NO) << "\n";
        if (nDevice.getBridge_dco_support()) {
            FileSaveTo << "DCO In Use:\t\t" << (nDevice.getBridge_dco_in_use() ? TP_YES : TP_NO) << "\n"
                    << "DCO Capacity:\t\t" << nDevice.getBridge_dco_capacity() << "\n"
                    << "DCO Challenge Key:\t\t" << nDevice.getBridge_dco_challenge_key() << "\n";


        }

    } else {
        FileSaveTo << "## Local Device Information ##\n"
                << "Vendor:\t" << nDevice.getVendor() << "\n"
                << "Model:\t" << nDevice.getModel() << "\n"
                << "Size:\t" << nDevice.getSize() << "\n"
                << "Blocksize:\t" << nDevice.getSizeBLK() << "\n";

    }
    FileSaveTo.close();
}

/**
 * @brief The choosen filename
 * @return choosen filename
 */
string SaveToFileDialog::get_Filename(){
    return sv_FileChooser.get_filename();
}

